package com.ruoyi.kiku.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kiku.mapper.KikuTokenConfigMapper;
import com.ruoyi.kiku.domain.KikuTokenConfig;
import com.ruoyi.kiku.service.IKikuTokenConfigService;

/**
 * 平台的cookie信息Service业务层处理
 *
 * @author TT
 * @date 2025-08-23
 */
@Service
public class KikuTokenConfigServiceImpl implements IKikuTokenConfigService
{
    @Autowired
    private KikuTokenConfigMapper kikuTokenConfigMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询平台的cookie信息
     *
     * @param id 平台的cookie信息主键
     * @return 平台的cookie信息
     */
    @Override
    public KikuTokenConfig selectKikuTokenConfigById(Long id)
    {
        return kikuTokenConfigMapper.selectKikuTokenConfigById(id);
    }

    /**
     * 查询平台的cookie信息列表
     *
     * @param kikuTokenConfig 平台的cookie信息
     * @return 平台的cookie信息
     */
    @Override
    public List<KikuTokenConfig> selectKikuTokenConfigList(KikuTokenConfig kikuTokenConfig)
    {
        kikuTokenConfig.setUserId(SecurityUtils.getUserId());
        return kikuTokenConfigMapper.selectKikuTokenConfigList(kikuTokenConfig);
    }

    /**
     * 新增平台的cookie信息
     *
     * @param kikuTokenConfig 平台的cookie信息
     * @return 结果
     */
    @Override
    public int insertKikuTokenConfig(KikuTokenConfig kikuTokenConfig)
    {
        kikuTokenConfig.setCreateTime(DateUtils.getNowDate());
        KikuTokenConfig result= disposeCookieAndUserId(kikuTokenConfig);
        return kikuTokenConfigMapper.insertKikuTokenConfig(result);
    }

    /**
     * 修改平台的cookie信息
     *
     * @param kikuTokenConfig 平台的cookie信息
     * @return 结果
     */
    @Override
    public int updateKikuTokenConfig(KikuTokenConfig kikuTokenConfig)
    {
        kikuTokenConfig.setUpdateTime(DateUtils.getNowDate());
        KikuTokenConfig result= disposeCookieAndUserId(kikuTokenConfig);
        return kikuTokenConfigMapper.updateKikuTokenConfig(result);
    }

    /**
     * 批量删除平台的cookie信息
     *
     * @param ids 需要删除的平台的cookie信息主键
     * @return 结果
     */
    @Override
    public int deleteKikuTokenConfigByIds(Long[] ids)
    {
        batchRemoveCookie(ids);
        return kikuTokenConfigMapper.deleteKikuTokenConfigByIds(ids);
    }

    /**
     * 删除平台的cookie信息信息
     *
     * @param id 平台的cookie信息主键
     * @return 结果
     */
    @Override
    public int deleteKikuTokenConfigById(Long id)
    {
        removeCookie(id);
        return kikuTokenConfigMapper.deleteKikuTokenConfigById(id);
    }
    /**
     * 处理cookie和userId
     * @param kikuTokenConfig
     * @return KikuTokenConfig
     */
    public KikuTokenConfig disposeCookieAndUserId(KikuTokenConfig kikuTokenConfig){
        String cookieValue = kikuTokenConfig.getCookieValue();
        Integer platform = kikuTokenConfig.getPlatform();
        Integer effectiveDuration = kikuTokenConfig.getEffectiveDuration();
        // 加密
        String encryptedValue  = Md5Utils.hash(cookieValue);
        Long userId = SecurityUtils.getUserId();
        redisCache.setCacheObject(platform+"_cookie:" + userId, cookieValue);
        redisCache.expire(platform+"_cookie:" + userId, effectiveDuration * 24 * 60 * 60);
        kikuTokenConfig.setCookieValue(encryptedValue);
        kikuTokenConfig.setUserId(userId);
        return kikuTokenConfig;
    }
    /**
     * 删除cookie
     * @param id
     */
    public void removeCookie(Long id){
        KikuTokenConfig kikuTokenConfig = kikuTokenConfigMapper.selectKikuTokenConfigById(id);
        Integer platform = kikuTokenConfig.getPlatform();
        Long userId = SecurityUtils.getUserId();
        redisCache.deleteObject(platform+"_cookie:" + userId);
    }
    /**
     * 批量删除cookie
     * @param ids
     */
    public void batchRemoveCookie(Long [] ids){
        Long userId = SecurityUtils.getUserId();
        for (Long id : ids) {
            KikuTokenConfig kikuTokenConfig = kikuTokenConfigMapper.selectKikuTokenConfigById(id);
            Integer platform = kikuTokenConfig.getPlatform();
            redisCache.deleteObject(platform+"_cookie:" + userId);
        }
    }
}
